package ro.imc.ums.dani.database;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import ro.imc.ums.dani.classes.Professor;


public class ProfessorCrud implements Crud {
	private Connection conn = null;

	public ProfessorCrud() {

	}

	@Override
	public void insert(Object o) {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/ums", "root", "");
			Statement sqlState = conn.createStatement();

			Professor professor = (Professor) o;

			String insert = "insert into professors"
					+ " (FirstName, LastName, ProfessorId, Cnp, Title, Field)"
					+ " values " + "('" + professor.getFirstName() + "','"
					+ professor.getLastName() + "','" + professor.getId() + "','"
					+ professor.getCnp() + "','" + professor.getTitle() + "','"
					+ professor.getField() + " ') ";

			sqlState.executeUpdate(insert);
		} catch (SQLException ex) {
			ex.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

	}

	@Override
	public ArrayList<Object> select() {
		ArrayList<Object> professorList = new ArrayList<Object>();
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/ums", "root", "");
			Statement sqlState = conn.createStatement();
			String select = "select * from professors";
			ResultSet result = sqlState.executeQuery(select);
			while (result.next()) {
				Professor professor = new Professor(result.getString(2),
						result.getString(3), Integer.parseInt(result
								.getString(4)), result.getString(5),
						result.getString(6), result.getString(7));
				professorList.add(professor);
			}
		} catch (SQLException ex) {
			ex.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return professorList;
	}

	@Override
	public void update() {
		// TODO Auto-generated method stub

	}

	@Override
	public void delete() {
		// TODO Auto-generated method stub

	}

	public ArrayList<Object> selectOne(String cnp){
		ArrayList<Object> professorList = new ArrayList<Object>();
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/ums", "root", "");
			Statement sqlState = conn.createStatement();
			String select = "select * from professors where cnp=" + cnp;
			ResultSet result = sqlState.executeQuery(select);
			while (result.next()) {
				Professor professor = new Professor(result.getString(2),
						result.getString(3), Integer.parseInt(result
								.getString(4)), result.getString(5),
						result.getString(6), result.getString(7));
				professorList.add(professor);
			}
		} catch (SQLException ex) {
			ex.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return professorList;
	}
}

